package au.com.uptick.serendipity.client.presenter;

import au.com.uptick.serendipity.client.NameTokens;
import au.com.uptick.serendipity.client.widgets.ApplicationMenu;
import au.com.uptick.serendipity.client.widgets.NavigationPane;
import au.com.uptick.serendipity.client.widgets.NavigationPaneHeader;
import au.com.uptick.serendipity.client.widgets.NavigationPaneSection;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.*;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.events.SectionHeaderClickEvent;
import com.smartgwt.client.widgets.layout.events.SectionHeaderClickHandler;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

public class MainPageSmartGwtPresenter extends Presenter<MainPageSmartGwtPresenter.MyView, MainPageSmartGwtPresenter.MyProxy> {

    private final PlaceManager placeManager;

    private static NavigationPaneHeader navigationPaneHeader;
    private static NavigationPane navigationPane;

    @ProxyCodeSplit
    @NameToken(NameTokens.mainSmartGwtPage)
    public interface MyProxy extends Proxy<MainPageSmartGwtPresenter>, Place {
    }

    public interface MyView extends View {
        ApplicationMenu getApplicationMenu();

        NavigationPaneHeader getNavigationPaneHeader();

        NavigationPane getNavigationPane();
    }

    /**
     * Use this in leaf presenters, inside their {@link #revealInParent} method.
     */
    @ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_SetContextAreaContent = new Type<RevealContentHandler<?>>();

    @Inject
    public MainPageSmartGwtPresenter(EventBus eventBus, MyView view, MyProxy proxy,
                                     PlaceManager placeManager) {
        super(eventBus, view, proxy);

        this.placeManager = placeManager;

        MainPageSmartGwtPresenter.navigationPaneHeader = view.getNavigationPaneHeader();
        MainPageSmartGwtPresenter.navigationPane = view.getNavigationPane();
    }

    @Override
    protected void onBind() {
        super.onBind();

        Log.debug("onBind()");

        // register Application Menu handler's
        getView().getApplicationMenu().addClickHandler("Sales", new ApplicationMenuClickHandler());

        // register Navigation Pane handler's
        getView().getNavigationPane().addSectionHeaderClickHandler(new NavigationPaneSectionHeaderClickHandler());
        getView().getNavigationPane().addRecordClickHandler("Sales", new NavigationPaneSectionClickHandler());
        getView().getNavigationPane().addRecordClickHandler("Settings", new NavigationPaneSectionClickHandler());
        getView().getNavigationPane().addRecordClickHandler("Resource Centre", new NavigationPaneSectionClickHandler());

        getView().getNavigationPane().expandSection("Sales");

        PlaceRequest myRequest = new PlaceRequest(NameTokens.activities);
        placeManager.revealPlace(myRequest);
    }

    @Override
    protected void onReveal() {
        super.onReveal();

        Log.debug("onReveal()");
    }

    @Override
    protected void onReset() {
        super.onReset();
    }

    @Override
    protected void revealInParent() {
        RevealRootLayoutContentEvent.fire(this, this);
    }

    public class ApplicationMenuClickHandler implements ClickHandler {
        @Override
        public void onClick(MenuItemClickEvent event) {

            MenuItem menuItem = event.getItem();
            String name = menuItem.getTitle();

            Log.debug("ApplicationMenuClickHandler.onClick() - " + name);
        }
    }

    public class NavigationPaneSectionHeaderClickHandler implements SectionHeaderClickHandler {
        @Override
        public void onSectionHeaderClick(SectionHeaderClickEvent event) {

            SectionStackSection section = event.getSection();
            String name = ((NavigationPaneSection) section).getSelectedRecord();

            Log.debug("NavigationPaneSectionHeaderClickHandler.onSectionHeaderClick() - " + name);

            if (name.length() != 0) {
                PlaceRequest myRequest = new PlaceRequest(name);
                placeManager.revealPlace(myRequest);
            }
        }
    }

    public class NavigationPaneSectionClickHandler implements RecordClickHandler {
        @Override
        public void onRecordClick(RecordClickEvent event) {

            Record record = event.getRecord();
            String name = record.getAttributeAsString("name");

            Log.debug("NavigationPaneClickHandler.onRecordClick() - " + name);

            PlaceRequest myRequest = new PlaceRequest(name);
            placeManager.revealPlace(myRequest);
        }
    }

    public static NavigationPaneHeader getNavigationPaneHeader() {
        return navigationPaneHeader;
    }

    public static NavigationPane getNavigationPane() {
        return navigationPane;
    }
}
